********************************************
* CONVERTING FILE FROM THE SPATIAL         *
* JOIN OF BUILDINGS AND PARCELS POLYGONS   *
* FROM SHAPEFILE TO DTA FILE               * 
********************************************
 
// Creates the buildings_on_parcel.dta and parcels_on_building.dta files 

clear all
set more off


// Working directories
cd "/Volumes/LaCie4TB/_EXTERNAL_WORKSPACE/crsh_land_use/data_march2024/"


// Locals we need for looping across years and IO directories
local years "2001 2003 2005 2007 2009 2011 2013 2017"
local input_path "/Volumes/LaCie4TB/_EXTERNAL_WORKSPACE/crsh_land_use/data_march2024/data_raw"
local output_path "/Volumes/LaCie4TB/_EXTERNAL_WORKSPACE/crsh_land_use/data_march2024/data_processed"
local temp_path "/Volumes/LaCie4TB/_EXTERNAL_WORKSPACE/crsh_land_use/data_march2024/temp"


// STEP 1: BUILDINGS

// Get a list of all shapefiles
local shape : dir . files"*.shp"                      // Remove the .shp extensions leaving just the file names
local filename : subinstr local shape ".shp" "", all  // Drop all shape extensions


// Loop through the list of file names
foreach file in `filename' {
	spshape2dta `file'.shp, replace
	use `file'.dta, clear
	tostring idsup_1 idsup, replace
	destring area_*, replace
	capture gen state_idb = "`file'"
	//sleep 500             //Avoiding errors such as ".... file is read-only and cannot be modified or erased"
	order _all, alphabetic	
	save `file'.dta, replace
	erase `file'_shp.dta
}


// Append files with fs function
capture erase paronbuild.dta
capture erase parcels_on_buildings.dta

cd "C:\Users\ag191004\OneDrive - UQAM\DataBase\merged_data\far\building" // [NOTE] We need to replace this
clear
fs *.dta
append using `r(files)', force

save "`temp_path'/paronbuild.dta", replace


// Create aggregated building area per parcel
use paronbuild.dta, clear

ren area_d PBB_area_d
ren area_c PBB_area_c
ren idsup_1 idparcelp
ren TARGET_FID idparcelb
ren area_d_1 PBP_area_d
ren area_c_1 PBP_area_c
ren Join_Count multiponb

sort state_idb idparcelb
collapse (sum) multiponb PBB_area_d PBB_area_c PBP_area_d PBP_area_c, by(state_idb idparcelb)

replace state_idb = substr(state_idb, 1, 2)
replace state_id = "alberta" if state_id == "ab"
replace state_id = "britishcolumbia" if state_id == "bc"
replace state_id = "manitoba" if state_id == "ma"
replace state_id = "quebec" if state_id == "qc"
replace state_id = "saskatchewan" if state_id == "sa"
replace state_id = "newbrunswick" if state_id == "nb"
replace state_id = "ontario" if state_id == "on"
sort state_idb idparcelb

duplicates drop state_idb idparcelb, force
save "`output_path'/parcels_on_building.dta", replace


// STEP 2: PARCELS

cd "C:\Users\ag191004\OneDrive - UQAM\DataBase\merged_data\far\parcel" // [NOTE] We need to replace this

// Get a list of shape files
local shape : dir . files"*.shp"                      // Remove the .shp extensions leaving just the file names
local filename : subinstr local shape ".shp" "", all  // Drop all shp extensions

// Loop through the list of file names
foreach file in `filename' {
	spshape2dta `file'.shp,replace
	use `file'.dta, clear
	tostring idsup_1 idsup, replace
	destring area_*, replace
	capture gen state_idp = "`file'"
	sleep 500        //Avoid errors such as ".... file is read-only and cannot be modified or erased"
	order _all, alphabetic	
	save `file'.dta, replace
	erase `file'_shp.dta
}


// Append files with fs function
capture erase buildonpar.dta
capture erase buildings_on_parcel.dta
cd "C:\Users\ag191004\OneDrive - UQAM\DataBase\merged_data\far\parcel" // [NOTE] We need to replace this
clear
fs *.dta
append using `r(files)', force
save "`temp_path'/buildonpar.dta", replace

// Create aggregate building area per parcel
use buildonpar.dta, clear
ren area_d BPP_area_d
ren area_c BPP_area_c
ren idsup_1 idparcelb
ren idsup idparcelp
ren area_d_1 BPB_area_d
ren area_c_1 BPB_area_c
ren Join_Count multibuild

sort state_idp idparcelp
collapse (sum) multibuild BPP_area_d BPP_area_c BPB_area_d BPB_area_c, by(state_idp idparcelp)

replace state_idp = subinstr(state_idp, "_", "", .)
sort state_idp idparcelp
save "`output_path'/buildings_on_parcel.dta", replace
